package cn.iocoder.yudao.module.etl.controller.admin.dataset;

import cn.iocoder.yudao.module.etl.dal.dataobject.DatasetTableTask;
import cn.iocoder.yudao.module.etl.request.BaseGridRequest;
import cn.iocoder.yudao.module.etl.request.dataset.DataSetTaskDTO;
import cn.iocoder.yudao.module.etl.request.dataset.DataSetTaskRequest;
import cn.iocoder.yudao.module.etl.service.dataset.DataSetTableTaskLogService;
import cn.iocoder.yudao.module.etl.service.dataset.DataSetTableTaskService;
import cn.iocoder.yudao.module.etl.utils.PageUtils;
import cn.iocoder.yudao.module.etl.utils.Pager;
import cn.iocoder.yudao.module.etl.utils.ResultHolder;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author gin
 * @Date 2021/3/4 1:32 下午
 */
@Api(tags = "数据集：数据集任务")
@ApiSupport(order = 90)
@RestController
@RequestMapping("etl/dataset/task")
public class DataSetTableTaskController {
    @Resource
    private DataSetTableTaskService dataSetTableTaskService;
    @Resource
    private DataSetTableTaskLogService dataSetTableTaskLogService;

    @ApiOperation("保存")
    @PostMapping("save")
    public ResultHolder save(@RequestBody DataSetTaskRequest dataSetTaskRequest) throws Exception {
        DatasetTableTask save = dataSetTableTaskService.save(dataSetTaskRequest);
        return ResultHolder.success(save);
    }

    //TODO
    @ApiOperation("删除")
    @PostMapping("delete/{id}")
    public void delete(@PathVariable String id) {
        dataSetTableTaskService.delete(id);
    }

    @ApiOperation("查询")
    @PostMapping("list")
    public ResultHolder list(@RequestBody DatasetTableTask datasetTableTask) {
        List<DatasetTableTask> list = dataSetTableTaskService.list(datasetTableTask);
        return ResultHolder.success(list);
    }

    @ApiOperation("分页查询")
    @PostMapping("/pageList/{goPage}/{pageSize}")
    public Pager<List<DataSetTaskDTO>> taskList(@PathVariable int goPage, @PathVariable int pageSize, @RequestBody BaseGridRequest request) {
        Page<Object> page = new Page<>(goPage,pageSize);
        return PageUtils.setPageInfo(page, dataSetTableTaskService.taskList4User(request));
    }

    @ApiIgnore
    @PostMapping("/lastExecStatus")
    public ResultHolder lastExecStatus(@RequestBody DataSetTaskDTO datasetTableTask) {
        DataSetTaskDTO dataSetTaskDTO = dataSetTableTaskLogService.lastExecStatus(datasetTableTask);
        return  ResultHolder.success(dataSetTaskDTO);
    }

    @ApiOperation("更新状态")
    @PostMapping("/updateStatus")
    public void updateStatus(@RequestBody DatasetTableTask datasetTableTask) throws Exception{
        dataSetTableTaskService.updateDatasetTableTaskStatus(datasetTableTask);
    }

    @ApiOperation("执行任务")
    @PostMapping("/execTask")
    public void execTask(@RequestBody DatasetTableTask datasetTableTask) throws Exception{
        dataSetTableTaskService.execTask(datasetTableTask);
    }

}
